package com.train.mgr.modules.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.train.common.exception.BizException;
import com.train.common.model.PageResult;
import com.train.common.utils.AuthUtils;
import com.train.common.utils.StringUtils;
import com.train.mgr.modules.biz.dto.TraineeCourseDetailVo;
import com.train.mgr.modules.biz.dto.TraineeStuExerciseDTO;
import com.train.mgr.modules.biz.dto.TraineeStudyExerciseSearchVo;
import com.train.mgr.modules.biz.dto.request.TraineeExercisePageDTO;
import com.train.mgr.modules.biz.dto.request.UserExerciseReqDTO;
import com.train.mgr.modules.biz.dto.response.*;
import com.train.mgr.modules.biz.entity.LessonUserExercise;
import com.train.mgr.modules.biz.mapper.LessonUserExerciseMapper;
import com.train.mgr.modules.biz.service.ILessonUserExerciseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * <p>
 * 用户课时练习表 服务实现类
 * </p>
 *
 * @author cxg
 * @date 2025-09-30
 */
@Slf4j
@RequiredArgsConstructor
@Service("ILessonUserExerciseService")
public class LessonUserExerciseServiceImpl extends ServiceImpl<LessonUserExerciseMapper, LessonUserExercise> implements ILessonUserExerciseService {
    @Autowired
    private LessonUserExerciseMapper lessonUserExerciseMapper;

    @Override
    public List<TraineeExerciseDTO> getCourseHomeExercise(Long courseId, Long lessonId,Integer exerciseType,String orderBy) {
        return lessonUserExerciseMapper.getCourseHomeExercise(courseId,lessonId,exerciseType, AuthUtils.getCurrentUser().getId(),orderBy);
    }

    @Override
    public List<LessonUserExercise> findLessonUserExerciseList(Long customerId,Long courseId,Long userId){
        LambdaQueryWrapper<LessonUserExercise> queryStudyWrapper = new LambdaQueryWrapper<LessonUserExercise>();
        queryStudyWrapper.eq(LessonUserExercise::getCustomerId,customerId);
        queryStudyWrapper.eq(LessonUserExercise::getCoruseId,courseId);
        queryStudyWrapper.eq(LessonUserExercise::getUserId,userId);
        queryStudyWrapper.eq(LessonUserExercise::getIsDeleted,0);
        return baseMapper.selectList(queryStudyWrapper);
    }

    @Override
    public List<LessonUserExercise> getByProjectTypeLessonId(Integer exerciseType, Long lessonId) {
        LambdaQueryWrapper<LessonUserExercise> queryStudyWrapper = new LambdaQueryWrapper<LessonUserExercise>();
        queryStudyWrapper.eq(LessonUserExercise::getCustomerId,AuthUtils.getCurrentUser().getCustomerId())
                            .eq(LessonUserExercise::getLessonId,lessonId)
                            .eq(LessonUserExercise::getUserId,AuthUtils.getCurrentUser().getId())
                            .eq(LessonUserExercise::getIsDeleted,0)
                            .eq(LessonUserExercise::getExerciseType,exerciseType)
                            .in(LessonUserExercise::getIsGenerateReports,1,2)
                            .orderByDesc(LessonUserExercise::getExerciseNumber);
        return baseMapper.selectList(queryStudyWrapper);
    }

    /**
     * 课程学员管理的跳转学习记录列表（课程ID必填）
     * 员工列表的跳转过来的学习记录（用户ID必填））
     * @param params
     * @return
     */
    @Override
    public PageResult<UserExerciseRespListDTO> userExerciseList(UserExerciseReqDTO params){
        params.setCustomerId(AuthUtils.getCurrentUser().getCustomerId());
        Page<UserExerciseRespListDTO> page = params.buildPage();
        Page<UserExerciseRespListDTO> resultPage = baseMapper.selectUserExerciseList(page,params);
        return PageResult.of(resultPage, UserExerciseRespListDTO.class);
    }

    @Override
    public Page<TraineeAllExerciseDTO> getMyExerciseList(Page<TraineeAllExerciseDTO> page, TraineeExercisePageDTO dto) {
        return baseMapper.getMyExerciseList(page,dto);
    }

    @Override
    public List<TraineeExerciseDTO> getReport(Long exerciseId) {
        return baseMapper.getReport(exerciseId,AuthUtils.getCurrentUser().getId());
    }

    @Override
    public List<TraineeStudyExerciseSearchVo> getExerciseSearch(Long userId) {
        return baseMapper.getExerciseSearch(userId);
    }

    @Override
    public TraineeStuRespDTO getExerciseStuCount() {
        return baseMapper.getExerciseStuCount(AuthUtils.getCurrentUser().getId());
    }

    @Override
    public List<TraineeStuExerciseDTO> getExerciseStuCountList() {
        return baseMapper.getExerciseStuCountList(AuthUtils.getCurrentUser().getId());
    }

}
